package cn.xiaoyu.su.manager.impl;

import cn.xiaoyu.su.manager.ShortLinkManager;
import cn.xiaoyu.su.mapper.ShortLinkMapper;
import cn.xiaoyu.su.model.ShortLinkDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ShortLinkManagerImpl implements ShortLinkManager {

    @Autowired
    private ShortLinkMapper shortLinkMapper;
    /**
     * 新增
     * @param shortLinkDO
     * @return
     */
    @Override
    public int addShortLink(ShortLinkDO shortLinkDO) {

        return shortLinkMapper.insert(shortLinkDO);
    }

    /**
     * 查找
     * @param shortLinkCode
     * @return
     */
    @Override
    public ShortLinkDO findByShortLinkCode(String shortLinkCode) {
        return shortLinkMapper.selectOne(new QueryWrapper<ShortLinkDO>().eq("code",shortLinkCode).eq("del",0));
    }

    /**
     * 删除
     * @param shortLinkDO
     * @return
     */
    @Override
    public int del(ShortLinkDO shortLinkDO) {
        //逻辑删除
        return shortLinkMapper.update(null,new UpdateWrapper<ShortLinkDO>()
                .eq("code",shortLinkDO.getCode())
                .eq("account_no",shortLinkDO.getAccountNo()).set("del",1));
    }

    /**
     * 更新
     * @param shortLinkDO
     * @return
     */
    @Override
    public int update(ShortLinkDO shortLinkDO) {

        return shortLinkMapper.update(null,new UpdateWrapper<ShortLinkDO>()
                .eq("code",shortLinkDO.getCode())
                .eq("del",0)
                .eq("account_no",shortLinkDO.getAccountNo())

                .set("title",shortLinkDO.getTitle())
                .set("domain",shortLinkDO.getDomain()));
    }
}
